package com.thx.dictionary.action;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.common.struts2.CurdAction;
import com.thx.common.web.ActionResult;
import com.thx.dictionary.model.Dictionary;
import com.thx.dictionary.model.DictionaryValue;
import com.thx.dictionary.service.DictionaryManager;
import com.thx.dictionary.service.DictionaryValueManager;
import com.thx.log.LogUtil;

@Namespace("/sys")
@Action(value = "dictName", results = { @Result(name = "input", location = "/pages/system/dictionary/input.jsp"),
		@Result(name = "list", location = "/pages/system/dictionary/list.jsp"),
		@Result(name = "view", location = "/pages/system/dictionary/detail.jsp"),
		@Result(name = "json", type = "json", params = { "root", "result" }) })
@Controller
@Scope("prototype")
public class DictionaryAction extends CurdAction<Dictionary> {

	private static final long serialVersionUID = 1L;

	protected String[] dicvTexts;
	protected String[] dicValues;
	protected String[] dicvIds;

	@Autowired
	protected DictionaryManager dicnMgr;
	@Autowired
	protected DictionaryValueManager dicvMgr;

	public DictionaryAction() {
	}

	protected void doList() throws Exception {
		list = dicnMgr.searchDicName(entity.getDicName());
	}

	protected void doSave() throws Exception {
		try {
			if (StringUtils.isBlank(id)) {
				saveDicn();
				setResult(ActionResult.SUCCESS, "添加成功！");
				LogUtil.log("字典模块", LogUtil.ACTION_ADD, LogUtil.RESULT_SUCCESS, "新增字典");

			} else {
				editDicn();
				setResult(ActionResult.SUCCESS, "修改成功！");
				LogUtil.log("字典模块", LogUtil.ACTION_UPDATE, LogUtil.RESULT_SUCCESS, "修改字典");
			}
		} catch (Exception e) {
			if (StringUtils.isBlank(id)) {
				setResult(ActionResult.FAIL, "添加失败！");
			} else {
				setResult(ActionResult.FAIL, "修改失败！");
			}
			logger.error(e.getMessage(), e);
		}
	}

	protected void doView() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			entity = dicnMgr.get(id);
			if (entity == null) {
				setResult(ActionResult.FAIL, "要查看的记录不存在！！！");
			}
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	protected void doInput() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			entity = dicnMgr.get(id);
			if (entity == null) {
				setResult(ActionResult.FAIL, "要修改的记录不存在！！！");
			}
		}
	}

	protected void doDelete() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			dicnMgr.delete(id);
			setResult(ActionResult.SUCCESS, "删除成功！");
			LogUtil.log("字典模块", LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除字典");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	protected void doDelMulti() throws Exception {
		if (delId != null && delId.length > 0) {
			delId = delId[0].split(",");
			dicnMgr.delMulti(delId);
			setResult(ActionResult.SUCCESS, "删除成功！");
			LogUtil.log("字典模块", LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除字典");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	public String deleteDicv() {
		try {
			if (StringUtils.isNotBlank(id)) {
				dicvMgr.delete(id);
				setResult(ActionResult.SUCCESS, "删除成功！");
				LogUtil.log("字典模块", LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除字典值");
			} else {
				setResult(ActionResult.FAIL, "服务器异常！");
			}
		} catch (Exception e) {
			setResult(ActionResult.FAIL, "删除失败！！！");
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
		return JSON;
	}

	/**
	 * 验证字段是否存在
	 */
	public void checkDicName() {
		try {
			Dictionary dicn = dicnMgr.findByDicName(entity.getDicName());
			if (dicn == null) {
				writeJson("true");
			} else {
				if (StringUtils.isNotBlank(id) && id.equals(dicn.getDicnId())) {
					// --修改时用户名和原来的可以保持一致
					writeJson("true");
				} else {
					writeJson("false");
				}

			}

		} catch (Exception e) {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	public void saveDicn() {
		entity.setDicName(StringUtils.strip(entity.getDicName()));
		entity.setDicnCode(StringUtils.strip(entity.getDicnCode()));
		entity.setDicnType("0");
		entity.setInUse("0");
		entity.setCreateDate(new Date());
		if (dicvTexts != null && dicValues != null) {
			for (int i = 0; i < dicvTexts.length; i++) {
				if (StringUtils.isNotBlank(dicvTexts[i]) && StringUtils.isNotBlank(dicValues[i])) {
					DictionaryValue dicv1 = new DictionaryValue();
					dicv1.setDicvOrder(i + 1 + "");
					dicv1.setDicn(entity);
					dicv1.setDicvText(StringUtils.strip(dicvTexts[i]));
					dicv1.setDicValue(StringUtils.strip(dicValues[i]));
					entity.addDicValues(dicv1);
				}
			}
		}
		dicnMgr.save(entity);
	}

	public void editDicn() {
		Dictionary dicn = dicnMgr.get(id);
		if (dicvIds != null && dicvTexts != null && dicValues != null) {
			for (int i = 0; i < dicvIds.length; i++) {
				if (StringUtils.isBlank(dicvIds[i]) && StringUtils.isNotBlank(dicvTexts[i])
						&& StringUtils.isNotBlank(dicValues[i])) {
					DictionaryValue dicv2 = new DictionaryValue();
					dicv2.setDicn(dicn);
					dicv2.setDicvText(StringUtils.strip(dicvTexts[i]));
					dicv2.setDicValue(StringUtils.strip(dicValues[i]));
					dicv2.setDicvOrder(i + 1 + "");
					dicn.addDicValues(dicv2);
				} else if (StringUtils.isNotBlank(dicvIds[i]) && StringUtils.isNotBlank(dicvTexts[i])
						&& StringUtils.isNotBlank(dicValues[i])) {
					DictionaryValue dicv = dicvMgr.get(dicvIds[i]);
					dicv.setDicvOrder(i + 1 + "");
					dicv.setDicvText(StringUtils.strip(dicvTexts[i]));
					dicv.setDicValue(StringUtils.strip(dicValues[i]));
					dicn.addDicValues(dicv);
				}

			}

		}
		dicn.setDicName(StringUtils.strip(entity.getDicName()));
		dicn.setDicnCode(StringUtils.strip(entity.getDicnCode()));
		dicnMgr.save(dicn);
	}

	public DictionaryManager getDicnMgr() {
		return dicnMgr;
	}

	public void setDicnMgr(DictionaryManager dicnMgr) {
		this.dicnMgr = dicnMgr;
	}

	public DictionaryValueManager getDicvMgr() {
		return dicvMgr;
	}

	public void setDicvMgr(DictionaryValueManager dicvMgr) {
		this.dicvMgr = dicvMgr;
	}

	public String[] getDicvTexts() {
		return dicvTexts;
	}

	public void setDicvTexts(String[] dicvTexts) {
		this.dicvTexts = dicvTexts;
	}

	public String[] getDicValues() {
		return dicValues;
	}

	public void setDicValues(String[] dicValues) {
		this.dicValues = dicValues;
	}

	public String[] getDicvIds() {
		return dicvIds;
	}

	public void setDicvIds(String[] dicvIds) {
		this.dicvIds = dicvIds;
	}

}